﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：ChildUserList.cs
    // 文件功能描述：子用户列表 锁定、解锁、删除子用户
    // 创建标识：吕海乐2009-05-13
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;


public partial class ChildUserList : BasePage
{
    public ChildUserList()
    {
        this.ModuleNo = Module.用户信息管理.ToString();
        this.PageOperateType = OperateType.子用户管理.ToString();
    }

    IBLL_User iu = BLLFactory.BLL_User(); 
    KeyOperationLogJG kj;
    string s = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //核心页面，使用强制权限限定
        if (!CurrentAccount.GetUser().IsAdmin)
        {
            PageError("您不是管理员，不能管理子用户！", "../PageControl/Right_Index.aspx");
            return;
        }

        if (!IsPostBack)
        {
            StoreCondition();
            bindUser();
        }
    }

    /// <summary>
    /// 定义绑定用户的方法
    /// </summary>
    private void bindUser()
    {
        //通过GetUser方法获取用户的Org
        User currentUser = CurrentAccount.GetUser();
        Org org = currentUser.Org;
        //判断用户所属机构是否为空
        if (org != null)
        {
            //获取查询条件，执行查询
            IBLL_User iu = HZ.Supervise.BLLFactory.BLL_User();
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalRecord = 0;
            HZ.Supervise.Model.Query.User queryUser = (HZ.Supervise.Model.Query.User)ViewState["v_user"];
            this.gvwChildUserList.PageSize = AspNetPager1.PageSize;
            this.gvwChildUserList.DataSource = iu.GetUserByWhere(queryUser, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalRecord);
            AspNetPager1.RecordCount = totalRecord;
            this.gvwChildUserList.DataBind();
        }
        else
        {
            PageError("用户无所属机构，无法查看其子用户！", "");
        }
    }

    private void StoreCondition()
    {
        //通过GetUser方法获取用户的Org
        User currentUser = CurrentAccount.GetUser();
        Org org = currentUser.Org;
        if (org != null)
        {
            HZ.Supervise.Model.Query.User user = new HZ.Supervise.Model.Query.User();
            user.IsAdmin = false;
            user.OrgID = org.OrgID;
            user.RoleID = currentUser.UserRole.RoleID;
            user.Username = this.txtChildUserName.Text.Trim();
            user.Userid = this.txtUserId.Text.Trim();
            if (this.ddlIsUsing.SelectedItem.Value.ToString() == "0")
            {
                user.Isusing = false;
            }
            else if (this.ddlIsUsing.SelectedItem.Value.ToString() == "1")
            {
                user.Isusing = true;
            }
            else
            {
                user.Isusing = null;
            } 
            //if (this.ddlIsOnline.SelectedItem.Value.ToString() == "0")
            //{
            //    user.Isonline = false;
            //}
            //else if (this.ddlIsOnline.SelectedItem.Value.ToString() == "1")
            //{
            //    user.Isonline = true;
            //}
            //else
            //{
            //    user.Isonline = null;
            //}
            if (HZ.Common.MyValidate.IsDate(this.txtLoginFrom.Text.Trim()))
            {
                user.LastlogintimeFrom = Convert.ToDateTime(txtLoginFrom.Text.Trim());
            }
            if (HZ.Common.MyValidate.IsDate(txtLoginTo.Text.Trim()))
            {
                user.LastlogintimeTo = Convert.ToDateTime(txtLoginTo.Text.Trim());
            }
            if (this.ddlIsJK.SelectedItem.Value.ToString() == "0")
            {
                user.IsJK = false;
            }
            else if (this.ddlIsJK.SelectedItem.Value.ToString() == "1")
            {
                user.IsJK = true;
            }
            else
            {
                user.IsJK = null;
            } 
            user.SortFields = "Userid ASC";
            ViewState["v_user"] = user;
        }

    }

    /// <summary>
    /// 调用方法，绑定用户
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        AspNetPager1.CurrentPageIndex = 1;
        StoreCondition();
        bindUser();
    }

    /// <summary>
    /// 添加子用户
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddChildUser_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("ChildUserAdd.aspx");
    }

    /// <summary>
    /// 删除角色
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvwChildUserList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            string strUserID = e.CommandArgument.ToString();

            int flag = iu.DeleteUser(strUserID);
            if (flag == 0)
            {
                HZ.Web.MessageBox.Show("删除失败！");
            }
            if (flag == 1)
            {
                AddLog(string.Format("删除子用户[{0}]成功!", strUserID));
                HZ.Web.MessageBox.AlertAndRefresh("删除成功！");
            }
            if (flag == 2)
            {
                HZ.Web.MessageBox.Show("该用户不存在！");
            }
        }
    }

    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
    {
        bindUser();
    }

    /// <summary>
    /// 取消
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvwUser_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.gvwChildUserList.EditIndex = -1;
        this.bindUser();
    }

    /// <summary>
    /// 绑定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvwChildUserList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    User aUser = iu.GetUserByUserID(e.Row.Cells[1].Text.Trim());
        //    LinkButton lb = (LinkButton)e.Row.Cells[4].FindControl("lbnLock");
        //    if (lb != null)
        //    {
        //        if (aUser.IsUsing)
        //        {
        //            lb.Text = "锁定";
        //        }
        //        else
        //        {
        //            lb.Text = "解锁";
        //        }
        //    }
        //} 
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvwChildUserList_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.gvwChildUserList.EditIndex = e.NewEditIndex;
        this.bindUser();
    }
    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvwChildUserList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (Page.IsValid)
        {
           int rowIndex = e.RowIndex;
            string userId = this.gvwChildUserList.DataKeys[rowIndex].Value.ToString();
            string strUserName = ((TextBox)this.gvwChildUserList.Rows[rowIndex].FindControl("txtUserName")).Text.Trim();
            CheckBox ck = (CheckBox)gvwChildUserList.Rows[rowIndex].FindControl("cbIsJK");
            if (string.IsNullOrEmpty(strUserName))
            {
                HZ.Web.WebUtil.Alert("用户名称不能为空！", this);
                ((TextBox)gvwChildUserList.Rows[rowIndex].FindControl("txtUserName")).Focus();
                return;
            }
            User user = new User();
            user.UserID = userId;
            user.UserName = strUserName;
            if (ck.Checked)
            {
                user.IsJK = true;
            }
            else
            {
                user.IsJK = false;
            }
            if (iu.UpdateUser(user) == 1)
            {
                int jk = iu.UpdateUser_IsJK(user);
                if (jk > 0)
                {
                    AddLog(string.Format("更改子用户[{0}]名称成功!", userId));
                    //重新绑定列表
                    this.gvwChildUserList.EditIndex = -1;
                    this.bindUser();
                }
                else
                {
                    PageError("更新子用户【是否疾控】状态失败！", "");              
                }
            }
            else
            {
                PageError("更新失败！", "");
            }
        }
    }

    /// <summary>
    /// 批量锁定
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnBatLock_Click(object sender, EventArgs e)
    {
        string strChkUsers = Request["chkChildUser"];//获取选中的用户ID，形式如 01,02,03
        if (!string.IsNullOrEmpty(strChkUsers))
        {
            string[] arryUsers = strChkUsers.Split(new char[',']);//分割成数组
            List<string> userIds = new List<string>();            //添加到List中
            for (int i = 0; i < arryUsers.Length; i++)
            {
                userIds.Add(arryUsers[i]);
            }

            //批量锁定
            if (iu.LockUser(userIds))
            {
                foreach (string u in userIds)
                {
                    s += u + ",";
                }
                s = s.Trim(',');
                AddLog(string.Format("批量停用子用户[{0}]成功!",  s));
                bindUser();
                //PageInfo("成功锁定选定用户！", "");
            }
            else
            {
                PageError("锁定用户失败！", "");
            }
        }
    }

    /// <summary>
    /// 批量初始化密码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnBatResetPws_Click(object sender, EventArgs e)
    {
        string strChkUsers = Request["chkChildUser"];
        if (!string.IsNullOrEmpty(strChkUsers))
        {
            string[] arryUsers = strChkUsers.Split(new char[',']);
            List<string> userIds = new List<string>();
            for (int i = 0; i < arryUsers.Length; i++)
            {
                userIds.Add(arryUsers[i]);
            }

            if (iu.InitPassword(userIds, Constants.USER_DEFAULT_PWS))
            {
                foreach (string u in userIds)
                {
                    s += u + ",";
                }
                s = s.Trim(',');
                AddLog(string.Format("批量初始化子用户[{0}]密码成功!", s));
                PageInfo("用户" + strChkUsers + "的密码已被初始化为" + Constants.USER_DEFAULT_PWS, "");
            }
            else
            {
                PageError("密码初始化失败", "");
            }
        }
    }

    /// <summary>
    /// 批量解锁
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnBatUnLock_Click(object sender, EventArgs e)
    {
        string strChkChildUsers = Request["chkChildUser"];
        if (!string.IsNullOrEmpty(strChkChildUsers))
        {
            string[] arryUsers = strChkChildUsers.Split(new char[',']);
            List<string> userIds = new List<string>();
            for (int i = 0; i < arryUsers.Length; i++)
            {
                userIds.Add(arryUsers[i]);
            }

            //批量解锁
            if (iu.UnblockUser(userIds))
            {
                foreach (string u in userIds)
                {
                    s += u + ",";
                }
                s = s.Trim(',');
                AddLog(string.Format("批量启用子用户[{0}]成功!", s));
                bindUser();
                // PageInfo("成功解锁选定用户！", "");
            }
            else
            {
                PageError("解锁用户失败！", "");
            }
        }
    }
    /// <summary>
    /// 添加日志
    /// </summary>
    /// <param name="msg"></param>
    private void AddLog(string msg)
    {
        kj = new KeyOperationLogJG();
        kj.OperationLogSerialID = Guid.NewGuid().ToString();
        kj.Operator = CurrentAccount.GetUser().UserID;
        kj.OrgID = CurrentAccount.GetUser().Org.OrgID;
        kj.OperateTime = DateTime.Now;
        kj.OperateMessage = msg;
        iu.AddKeyOperationLog(kj);
    }
    /// <summary>
    /// 简单查询与高级查询间的切换
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    //protected void lbnSwitch_Click(object sender, EventArgs e)
    //{
    //    if (lbnSwitch.CommandArgument == "simple")
    //    {
    //        ltlAdvance.Text = "";
    //        lbnSwitch.CommandArgument = "advance";
    //        lbnSwitch.Text = "简单查询";
    //    }
    //    else
    //    {
    //        ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
    //        lbnSwitch.CommandArgument = "simple";
    //        lbnSwitch.Text = "高级查询";

    //        //清空数据
    //        txtLoginFrom.Text = "";
    //        txtLoginTo.Text = "";
    //        txtChildUserName.Text = "";
    //        ddlIsOnline.SelectedIndex = -1;
    //        ddlIsUsing.SelectedIndex = -1;
    //    }
    //}
}

